package com.kitty.game.rank.service.handler;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.rank.message.RespTopUser;
import com.kitty.game.rank.model.PersonInfo;
import com.kitty.game.rank.model.RankType;
import com.kitty.game.rank.model.pojo.RankListData;
import com.kitty.game.rank.model.record.RankRecord;
import com.kitty.game.rank.model.record.ShuaDaoRankRecord;
import lombok.extern.slf4j.Slf4j;
import org.nutz.dao.Dao;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;
import java.util.ArrayList;
import java.util.List;

/**刷道伏魔排行榜处理类*/
@Component
@Slf4j
public class FuMo2RankHandler extends RankHandler{
    @Override
    public void init() {
        Dao dao = SpringUtils.getBean(Dao.class);
        RankListData fuMoRankData = dao.fetch(RankListData.class, RankType.FU_MO2.name());
        if (fuMoRankData != null) {
            fuMoRankData.doAfterInit();
            List<RankRecord> rankData = fuMoRankData.getRankData();
            for (RankRecord rankRecord : rankData) {
                ShuaDaoRankRecord shuaDaoRankRecord = (ShuaDaoRankRecord)rankRecord;
                update(shuaDaoRankRecord.getGid(), shuaDaoRankRecord);
            }
        }

    }

    public void save() {
        List<RankRecord> records = getRecordsFrom(0, getRankSize());
        if (records == null || records.size() == 0) {return ;}

        List<RankRecord> shuaDaoRankRecords = new ArrayList<>(records.size());
        for (RankRecord rankRecord : records) {
            shuaDaoRankRecords.add((ShuaDaoRankRecord)rankRecord);
        }

        if (shuaDaoRankRecords.size() <= 0) {return ;}

        RankListData fuMoRankData = new RankListData(RankType.FU_MO2, shuaDaoRankRecords);
        fuMoRankData.doBeforeSave();

        Dao dao = SpringUtils.getBean(Dao.class);
        dao.insertOrUpdate(fuMoRankData);
    }

    @PreDestroy
    private void shutDown() {
        save();
        log.error("[{}]保存数据后关闭", this.getClass().getSimpleName());
    }

    public RespTopUser getRankResponse(short type) {
        RespTopUser respTopUser = new RespTopUser();
        respTopUser.setType(type);
        respTopUser.setCookie(new Long(System.currentTimeMillis() / 1000).intValue());
        respTopUser.setRequestType((byte) 1);
        List<PersonInfo> personInfoList = getPersonInfoList(getRecordsFrom(0, getRankSize()));
        respTopUser.setList(personInfoList);
        respTopUser.setSize((short) respTopUser.getList().size());
        return respTopUser;
    }

    public List<PersonInfo> getPersonInfoList(List<RankRecord> records) {
        List<PersonInfo> list = new ArrayList<>(records.size());
        for (RankRecord rankRecord : records) {
            ShuaDaoRankRecord shuaDaoRankRecord = (ShuaDaoRankRecord)rankRecord;
            PersonInfo personInfo = new PersonInfo();
            personInfo.setList(new ArrayList<>());
            personInfo.getList().add(new FiedValue(305, shuaDaoRankRecord.getGid()));
            personInfo.getList().add(new FiedValue(31, shuaDaoRankRecord.getLevel()));
            personInfo.getList().add(new FiedValue(855, shuaDaoRankRecord.getPartyName()));
            personInfo.getList().add(new FiedValue(1, shuaDaoRankRecord.getName()));
            personInfo.getList().add(new FiedValue(990, shuaDaoRankRecord.getTime()));
            personInfo.getList().add(new FiedValue(44, shuaDaoRankRecord.getPolar()));
            personInfo.getList().add(new FiedValue(40, shuaDaoRankRecord.getRoleIcon()));
            list.add(personInfo);
        }
        return list;
    }
}
